package com.yaorange.teaching.service.impl;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

import com.yaorange.common.core.exception.ServiceException;
import com.yaorange.common.core.utils.StringUtils;
import com.yaorange.common.core.utils.json.JsonUtils;
import com.yaorange.common.security.utils.SecurityUtils;
import com.yaorange.teaching.bo.HolidayBo;
import com.yaorange.teaching.domain.YaHoliday;
import com.yaorange.teaching.mapper.YaHolidayMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yaorange.teaching.mapper.YaHolidayConfigMapper;
import com.yaorange.teaching.domain.YaHolidayConfig;
import com.yaorange.teaching.service.IYaHolidayConfigService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 节假日配置Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-07
 */
@Service
public class YaHolidayConfigServiceImpl implements IYaHolidayConfigService
{
    public static final int GUOQING_INDEX = 6;

    @Autowired
    private YaHolidayConfigMapper yaHolidayConfigMapper;

    @Autowired
    private YaHolidayMapper yaHolidayMapper;
    /**
     * 查询节假日配置
     *
     * @param id 节假日配置主键
     * @return 节假日配置
     */
    @Override
    public YaHolidayConfig selectYaHolidayConfigById(Long id)
    {
        return yaHolidayConfigMapper.selectYaHolidayConfigById(id);
    }

    /**
     * 查询节假日配置列表
     *
     * @param yaHolidayConfig 节假日配置
     * @return 节假日配置
     */
    @Override
    public List<YaHolidayConfig> selectYaHolidayConfigList(YaHolidayConfig yaHolidayConfig)
    {
        return yaHolidayConfigMapper.selectYaHolidayConfigList(yaHolidayConfig);
    }

    /**
     * 新增节假日配置
     *
     * @param yaHolidayConfig 节假日配置
     * @return 结果
     */
    @Override
    public int insertYaHolidayConfig(YaHolidayConfig yaHolidayConfig)
    {
        return yaHolidayConfigMapper.insertYaHolidayConfig(yaHolidayConfig);
    }

    /**
     * 修改节假日配置
     *
     * @param holidayBos 节假日配置
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateYaHolidayConfig(List<HolidayBo> holidayBos)
    {
        
        // 从节假日列表中提取年份
        String year = getYearFromHolidays(holidayBos);
        
        // 删除指定年份的所有节假日和节假日配置
        yaHolidayConfigMapper.deleteHolidayByYear(year);
        yaHolidayConfigMapper.deleteHolidayConfigByYear(year);
        
        // 创建并保存新的节假日配置
        YaHolidayConfig yaHolidayConfig = YaHolidayConfig.builder()
            .year(year) // 设置年份
            .content(JsonUtils.writeValueAsString(holidayBos)) // 将节假日列表转换为JSON字符串
            .build();
        yaHolidayConfigMapper.insertYaHolidayConfig(yaHolidayConfig);
        
        // 根据节假日配置生成节假日详情，并批量保存
        List<YaHoliday> yaHolidays = generateHolidayDetails(holidayBos, yaHolidayConfig.getId());
        yaHolidayMapper.batchInsertYaHoliday(yaHolidays);
        
        // 返回操作结果
        return 1;
    }

    /**
     * 批量删除节假日配置
     *
     * @param ids 需要删除的节假日配置主键
     * @return 结果
     */
    @Override
    public int deleteYaHolidayConfigByIds(Long[] ids)
    {
        return yaHolidayConfigMapper.deleteYaHolidayConfigByIds(ids);
    }

    /**
     * 删除节假日配置信息
     *
     * @param id 节假日配置主键
     * @return 结果
     */
    @Override
    public int deleteYaHolidayConfigById(Long id)
    {
        return yaHolidayConfigMapper.deleteYaHolidayConfigById(id);
    }

    // 1. 提取年份获取逻辑到单独的方法
    private String getYearFromHolidays(List<HolidayBo> holidayBos) {
        HolidayBo gqBo = holidayBos.get(GUOQING_INDEX);
        String date = gqBo.getDate();
        if (StringUtils.isEmpty(date)) {
            throw new ServiceException("请填写国庆节的起始日期");
        }
        return date.substring(0, 4);
    }

    // 2. 提取节假日详情生成逻辑到单独的方法
    private List<YaHoliday> generateHolidayDetails(List<HolidayBo> holidayBos, Long configId) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        List<YaHoliday> yaHolidays = new ArrayList<>(30);
        
        for (HolidayBo bo : holidayBos) {
            LocalDate localDate = LocalDate.parse(bo.getDate(), formatter);
            
            for (int i = 0; i < bo.getCount(); i++) {
                YaHoliday yaHoliday = YaHoliday.builder()
                    .date(localDate.format(formatter))
                    .description(bo.getDescription())
                    .configId(configId)
                    .weekday(localDate.getDayOfWeek().getValue())
                    .createBy(SecurityUtils.getUsername())
                    .build();
                    
                yaHolidays.add(yaHoliday);
                localDate = localDate.plusDays(1);
            }
        }
        return yaHolidays;
    }
}
